package com.api.client.imp;


import java.util.Map;

import org.scribe.model.OAuthRequest;
import org.scribe.model.Verb;

import com.api.client.Method;
import com.api.client.Request;
import com.api.client.RequestFactory;
import com.api.client.Service;
import com.api.client.VariablePath;

public class OAuthRequestFactory implements RequestFactory{

	private final String host;
	
	public OAuthRequestFactory(String host){
		this.host = host;
	}
	
	public Request newRequest(Service service, Map<String, Object> pathParams, Map<String, Object> params) {

		VariablePath path = service.getPath();
		String url = host + path.getRealPath(pathParams);
		
		Method method = service.getMethod();
		Verb verb = Verb.valueOf(method.toString());
		OAuthRequest oAuthRequest = new OAuthRequest(verb,url);
		
		if(method == Method.GET || method == Method.DELETE){
			
			for(Map.Entry<String, Object> param : params.entrySet()){
				
				oAuthRequest.addQuerystringParameter(param.getKey(), String.valueOf(param.getValue()));
			}
		}
		else{
			for(Map.Entry<String, Object> param : params.entrySet()){
				
				oAuthRequest.addBodyParameter(param.getKey(), String.valueOf(param.getValue()));
			}
		}
		return new RequestImp(oAuthRequest);
	}
}
